package com.yikun.springbootinit.job.once;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yikun.springbootinit.model.entity.Post;
import com.yikun.springbootinit.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 获取初始帖子列表
 */
// todo 取消注释任务后，每次启动 springboot 项目时会执行一次 run 方法
//@Component
@Slf4j
public class FetchInitPostList implements CommandLineRunner {

    @Resource
    private PostService postService;

    @Override
    public void run(String... args) {
        // 1.获取数据
        String json = "{\"current\":1,\"pageSize\":8,\"sortField\":\"createTime\",\"sortOrder\":\"descend\",\"category\":\"文章\",\"reviewStatus\":1}";
        String url = "https://www.code-nav.cn/api/post/search/page/vo";

        String result = HttpRequest
                .post(url)
                .body(json)
                .execute()
                .body();
        System.out.println(result);

        // 2.json转对象
        Map<String, Object> map = JSONUtil.toBean(result, Map.class);

        JSONObject data = (JSONObject) map.get("data");
        JSONArray records = (JSONArray) data.get("records");

        List<Post> postList = new ArrayList<>(records.size());
        for (Object record : records) {

            JSONObject tmp = (JSONObject) record;
            Post post = new Post();
            post.setTitle(tmp.getStr("title"));
            post.setContent(tmp.getStr("content"));
            JSONArray tags = (JSONArray) tmp.get("tags");
            List<String> tagList = tags.toList(String.class);
            post.setTags(JSONUtil.toJsonStr(tagList));
            post.setUserId(1L);

            postList.add(post);
        }

        System.out.println(postList);

        // 3.数据入库
        if (postService.saveBatch(postList)) {
            log.info("获取初始化帖子列表成功: 条数{}", postList.size());
        } else {
            log.error("获取初始化帖子列表失败");
        }
    }
}
